package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JTextField;

import com.artisan.dao.ClassDao;
import com.artisan.model.StudentClass;
import com.artisan.util.StringUtil;

import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddStudentClassFrm extends JInternalFrame {
	private JTextField classNameTextField;
	private JTextArea classInfoTextArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddStudentClassFrm frame = new AddStudentClassFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddStudentClassFrm() {
		setFrameIcon(new ImageIcon(AddStudentClassFrm.class.getResource("/com/artisan/image/学生管理.png")));
		setClosable(true);
		setIconifiable(true);
		
		setTitle("添加班级信息");
		setBounds(100, 100, 450, 300);

		JLabel lblNewLabel = new JLabel("班级名称:");
		lblNewLabel.setIcon(new ImageIcon(AddStudentClassFrm.class.getResource("/com/artisan/image/班级名称.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		classNameTextField = new JTextField();
		classNameTextField.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("班级信息:");
		lblNewLabel_1.setIcon(new ImageIcon(AddStudentClassFrm.class.getResource("/com/artisan/image/班级介绍.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		classInfoTextArea = new JTextArea();

		JButton submitButton = new JButton("提交");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				submitClass(ae);
			}
		});
		submitButton.setIcon(new ImageIcon(AddStudentClassFrm.class.getResource("/com/artisan/image/确认.png")));
		submitButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JButton resetButton = new JButton("重置");
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValue(e);
			}
		});
		resetButton.setIcon(new ImageIcon(AddStudentClassFrm.class.getResource("/com/artisan/image/重置.png")));
		resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(52)
				.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(classInfoTextArea)
						.addComponent(classNameTextField, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(83, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup().addGap(108)
						.addComponent(submitButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
						.addGap(32)
						.addComponent(resetButton, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(99, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(40)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
						.addComponent(classNameTextField, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1)
						.addComponent(classInfoTextArea, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(submitButton, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(resetButton, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(16, Short.MAX_VALUE)));
		getContentPane().setLayout(groupLayout);

	}

	protected void submitClass(ActionEvent ae) {
		// TODO 自动生成的方法存根
		String className = classNameTextField.getText().toString();
		String classInfo = classInfoTextArea.getText().toString();
		if (StringUtil.isEmpty(className)) {
			JOptionPane.showMessageDialog(this, "班级名称不能为空!");
			return;
		}
		StudentClass sc1 = new StudentClass();
		sc1.setName(className);
		sc1.setInfo(classInfo);
		ClassDao classDao = new ClassDao();
		if (classDao.addClass(sc1)) {
			JOptionPane.showMessageDialog(this, "班级添加成功!");
		}else {
			JOptionPane.showMessageDialog(this, "班级添加失败!");
		}
		classDao.closeDao();
		resetValue(ae);
	}

	protected void resetValue(ActionEvent e) {
		// TODO 自动生成的方法存根
		classNameTextField.setText("");
		classInfoTextArea.setText("");
	}
}
